
package com.catsic.zcjc.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.catsic.base.BaseServiceImpl;
import com.catsic.base.exception.CatsicException;
import com.catsic.base.tree.TreeInfo;
import com.catsic.core.convert.Converts;
import com.catsic.core.entity.Organ;
import com.catsic.core.exception.CatsicExceptionCode;
import com.catsic.core.organ.service.OrganService;
import com.catsic.pub.drawer.service.DrawerService;
import com.catsic.pub.entity.Drawer;
import com.catsic.zcjc.condition.SystemCondition;
import com.catsic.zcjc.constants.ZcjcConstants;
import com.catsic.zcjc.convert.ZcjcConverts;
import com.catsic.zcjc.dao.SystemDao;
import com.catsic.zcjc.entity.Station;
import com.catsic.zcjc.entity.System;
import com.catsic.zcjc.exception.ZcjcExceptionCode;
import com.catsic.zcjc.form.SystemForm;
import com.catsic.zcjc.service.StationService;
import com.catsic.zcjc.service.SystemService;
import com.catsic.zcjc.specifiction.SystemSpec;


@Service("systemService")
public class SystemServiceImpl extends BaseServiceImpl<System, String> implements SystemService{
	
	/**
	 * SystemDao:站点Dao
	 */
	@Autowired
	private SystemDao systemDao;

	@Autowired
	private StationService stationService;
	
	@Autowired
	private OrganService organService;
	
	@Autowired
	private DrawerService drawerService;

	@Override
	public SystemForm add(SystemForm form) {
		checkSystemSame(form);
		
		Station station = stationService.findById(form.getStationId());
		System system = new System();

		system.setCode(form.getCode());
		system.setName(form.getName());
		system.setStation(station);
		system.setRemark(form.getRemark());
		system.setType(form.getType());
		system.setStatus(form.getStatus());
		
		system.setCreateId(form.getCreateId());
		system.setCreateName(form.getCreateName());
		system.setCreateId(form.getCreateId());
		system.setCreateName(form.getCreateName());
		
		
		return ZcjcConverts.convert(super.add(system));
	}
	

	@Override
	public SystemForm update(SystemForm form) {
		checkSystemSame(form);
		
		System system = findById(form.getId());

		system.setCode(form.getCode());
		system.setName(form.getName());
		system.setRemark(form.getRemark());
		system.setType(form.getType());
		system.setStatus(form.getStatus());
		
		return ZcjcConverts.convert(super.add(system));
	}
	

	@Override
	public System findById(String id){
		System System = getById(id);
		if(null == System){
			throw new CatsicException(CatsicExceptionCode.实体不存在, ZcjcConstants.SYSTEM);
		}
		return System;
	}
	

	@Override
	public SystemForm findSystemById(String id){
		System system = findById(id);
		SystemForm form = ZcjcConverts.convert(system);
		
		
		return form;
	}
	

	
	public void checkSystemSame(SystemForm form) {
		if(hasSameSystem(form)){
			throw new CatsicException(ZcjcExceptionCode.系统管理_编码重复);
		}
	}

	public boolean hasSameSystem(SystemForm form) {
		System system = systemDao.findByCode(form.getStationId(),form.getCode());
		if(form.getId()==null){
			if(system!=null){
				return true;
			}
		}else{
			if(system!=null&&!system.getId().equals(form.getId())){
				return true;
			}
		}
		
		return false;
	}
	
	public Page<SystemForm> findAll(SystemCondition condition, Pageable pageable){
		SystemSpec spec = new SystemSpec(condition);
		Page<System> page = systemDao.findAll(spec, pageable);
		List<SystemForm> listForm = new ArrayList<SystemForm>();
		for(System system :page.getContent()){
			listForm.add(ZcjcConverts.convert(system));
	    }
	    Page<SystemForm> page_ = new PageImpl<SystemForm>(listForm,pageable,page.getTotalElements());
	    return page_;
	}




	@Override
	public List<SystemForm> query(String stationId) {
		List<System> list = this.findByStationId(stationId);
		
		List<SystemForm> listForm = new ArrayList<SystemForm>();
		
		if(CollectionUtils.isNotEmpty(listForm)){
			for(System system :list){
				listForm.add(ZcjcConverts.convert(system));
		    }
		}
		
		return listForm;
	}


	
	@Override
	public void delete(String id){
		System system = findById(id);
		
		if(!CollectionUtils.isEmpty(system.getEquips())){
			throw new CatsicException(ZcjcExceptionCode.系统管理_系统下有设备_无法删除);
		}

		super.delete(system);
	}
	
	public List<TreeInfo> getTreeInfo(String id){
		List<Organ> list = organService.findByPid(id);
		List<TreeInfo> listForm = new ArrayList<TreeInfo>();
		
		if(id.indexOf("station")!=-1){
			List<System> systemList = this.findByStationId(id.substring(8));
			if(!CollectionUtils.isEmpty(systemList)){
				for(System system : systemList){
					TreeInfo treeInfo = ZcjcConverts.convertTree(system);
					listForm.add(treeInfo);
				}
			}
			
			return listForm;
		}
		
		if(!CollectionUtils.isEmpty(list)){
			for(Organ organ : list){
				TreeInfo treeInfo = Converts.convertTree(organ);
				listForm.add(treeInfo);
			}
		}else{
			//插叙机构下面是否有站点
			List<Station> stationList = stationService.findByOrganId(id);
			if(!CollectionUtils.isEmpty(stationList)){
				for(Station station : stationList){
					TreeInfo treeInfo = ZcjcConverts.convertTree(station);
					listForm.add(treeInfo);
				}
			}
		}
		return listForm;
	}


	@Override
	public List<System> findByStationId(String stationId) {
		return systemDao.findByStation(stationId);
	}

	
	@Override
	public void findFormatDrawer(SystemForm form){
		Drawer typeD = null;
		if(form.getType()!=null){
			typeD = drawerService.findByParentAndValue(ZcjcConstants.DRAWER_SYSTEM_TYPE, form.getType().toString());
			if(typeD!=null){
				form.setCnType(typeD.getName());
			}
		}
	}
}
